package com.example.demo.model.custom;

import com.aliyuncs.exceptions.ClientException;
import com.baidu.bas.annotation.fun.DataProxy;
import com.baidu.bas.core.service.IEruptDataService;
import com.baidu.bas.core.view.EruptModel;
import com.baidu.bas.db.dao.EruptDao;
import com.deepzero.mmma.channel.AccountType;
import com.deepzero.mmma.channel.sms.account.AliSmsAccount;
import com.deepzero.mmma.channel.sms.manager.AliSmsManager;
import com.example.demo.enums.AuditStatus;
import com.example.demo.model.Account;
import com.example.demo.model.SmsTemplate;
import com.example.demo.service.TryService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author liuzhenchuan@foxmail.com
 * @date 2020/12/29
 */
@Component
@Slf4j
public class SmsTemplateDataProxy implements DataProxy<SmsTemplate> {

    @Resource
    private IEruptDataService iEruptDataService;

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    private TryService tryService;

    @Resource
    private EruptDao eruptDao;

    @Override
    public void beforeUpdate(SmsTemplate smsTemplate) {
        val old = eruptDao.findById(SmsTemplate.class,smsTemplate.getId());
        //所有更新重置审核状态
        smsTemplate.setStatus(AuditStatus.AUDINING);
    }

    @Override
    public void afterAdd(SmsTemplate smsTemplate) {
        sendToAuditIfRequired(smsTemplate);
    }

    private void sendToAuditIfRequired(SmsTemplate smsTemplate) {
        Account account = tryService.getAccount(smsTemplate.getAccount().getId());
        boolean hasAudit = StringUtils.isNotEmpty(smsTemplate.getOuterId()) && smsTemplate.getStatus()==AuditStatus.FINISHED_SUCCESS;
        boolean isAliSms = AccountType.aliyunSMS.equals(account.getType());

        if(isAliSms && !hasAudit){
            send2aliyun(smsTemplate, account);
        }

    }

    private void send2aliyun(SmsTemplate smsTemplate, Account account) {
        val config = account.getConfig();
        AliSmsManager manager = new AliSmsManager((AliSmsAccount) config);
        try {
            val result = manager.addTemplate(smsTemplate.getType().toAliyunSmsType(),
                    smsTemplate.getName(), smsTemplate.getContent(), smsTemplate.getRemark());
            if(!result.isSuccess()){
                val errorInfo = result.getInfo();
                smsTemplate.setStatus(AuditStatus.FINISHED_FAILED);
                smsTemplate.setInfo(errorInfo);
                iEruptDataService.editData(new EruptModel(SmsTemplate.class), smsTemplate);
            }else{
                smsTemplate.setOuterId(result.getData());
                iEruptDataService.editData(new EruptModel(SmsTemplate.class), smsTemplate);
            }
        } catch (Exception e) {
            log.warn("提交aliyun短信模板任务[{}]失败.", smsTemplate,e);
            smsTemplate.setStatus(AuditStatus.FINISHED_FAILED);
            smsTemplate.setInfo("提交aliyun短信模板任务失败.");
            iEruptDataService.editData(new EruptModel(SmsTemplate.class), smsTemplate);
        }
    }

    @Override
    public void afterUpdate(SmsTemplate smsTemplate) {
        sendToAuditIfRequired(smsTemplate);
    }

}
